package com.zj.ecology

import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.support.annotation.FloatRange
import android.util.AttributeSet
import android.view.View

/**
 * Created by zhaojie on 2018/4/2.
 */

class BatteryView : View {

    //帽子
    private var hatePaint: Paint? = null
    //容器
    private var bodyPaint: Paint? = null
    //电量
    private var batteryPaint: Paint? = null

    private var hateRect: RectF = RectF()

    private var bodyRect: RectF = RectF()

    private var batteryRect: RectF = RectF()

    private val padding = dp2px(15.0f)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context?) : super(context)

    init {
        initPaint()
    }

    private fun initPaint() {
        hatePaint = Paint()
        bodyPaint = Paint()
        batteryPaint = Paint()
        hatePaint?.color = Color.WHITE
        bodyPaint?.color = Color.WHITE
        batteryPaint?.color = Color.WHITE
        hatePaint?.isAntiAlias = true
        bodyPaint?.isAntiAlias = true
        batteryPaint?.isAntiAlias = true
        hatePaint?.style = Paint.Style.FILL
        bodyPaint?.style = Paint.Style.STROKE
        batteryPaint?.style = Paint.Style.FILL
    }

    override fun draw(canvas: Canvas?) {
        super.draw(canvas)
        canvas?.save()
        drawHate(canvas)
        drawBody(canvas)
        drawBattery(canvas)
        canvas?.restore()
    }

    var hateWidth = dp2px(14.0f)
    var hateHeight = dp2px(3.0f)
    private fun drawHate(canvas: Canvas?) {
        var hateLeft = padding + (bodyWidth / 2.0f - hateWidth / 2.0f)
        hateRect.set(hateLeft, padding, hateLeft + hateWidth, padding + hateHeight)
        canvas?.drawRoundRect(hateRect, 6f, 6f, hatePaint)
    }

    var bodyWidth = dp2px(25.0f)
    var bodyHeight = dp2px(40.0f)
    private fun drawBody(canvas: Canvas?) {
        bodyRect.set(padding, padding + hateHeight, padding + bodyWidth, padding + hateHeight + bodyHeight)
        canvas?.drawRoundRect(bodyRect, 4f, 4f, bodyPaint)

    }

    private var battery = 0.8f

    fun setBattery(@FloatRange(from = 0.0, to = 1.0) cur: Float) {
        if (cur in 0.0f..1.0f) {
            battery = cur
            postInvalidate()
        }
    }

    private fun drawBattery(canvas: Canvas?) {
        var b_left = bodyRect.left + 2.0f
        var b_right = bodyRect.right - 2.0f
        var b_bottom = bodyRect.bottom - 2.0f
        var maxH = bodyRect.top + 2.0f

        var batteryMaxH = b_bottom - maxH
        var b_top = b_bottom - (battery * batteryMaxH)
        batteryRect.set(b_left, b_top, b_right, b_bottom)
        canvas?.drawRoundRect(batteryRect, 4f, 4f, batteryPaint)

    }


    fun dp2px(dpValue: Float): Float {
        return (0.5f + dpValue * Resources.getSystem().displayMetrics.density)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension((padding * 2.0f + bodyWidth).toInt(), (padding * 2 + hateHeight + bodyHeight).toInt())
    }
}
